<template>
  <div>
    <!-- 大客户报备-->
    <TopFilter ref="topform" :list="topFilterList" :cols="3" @filterChange="changeHandle" @resetChange="changeHandle" :labelWidth="110" />
    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTables
        ref="table"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :maxHeight="800"
        :datakey="'records'"
        columnsRef="KeyCustomerInfo"
        :isSelectColumn="false"
        :onColumnsChange="(columns) => (this.columns = columns)"
      >
        <template slot="controls" slot-scope="props">
          <multiuse-button size="small" type="primary" @click="addHandle">新 增</multiuse-button>
        </template>
      </FilterTables>
    </Spin>
    <Drawer :visible.sync="detailVisible" :title="'大客户报备信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="72%">
      <detailModel :type="deailType" :rowData="formData" @close="detailcloseHandler" />
    </Drawer>
  </div>
</template>
<script>
import { getList, deleteBigCustomer } from '@/api/omSaleP/KeyCustomers/KeyCustomerInfo/index.js';
import { exportExcelFile } from '@/utils';
import detailModel from './detailModel';
import { dictionary } from '@/utils/dictMixin';
export default {
  name: 'KeyCustomerInfo',
  data() {
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchParams: {
        isDealer: 10041002,
      },
      fetchApi: getList,
      formData: {},
      deailType: '',
      exportLoading: false,
      issearch: false,
      detailVisible: false,
    };
  },
  components: {
    detailModel,
  },
  mixins: [dictionary],
  mounted() {},
  created() {},
  methods: {
    changeHandle(params) {
      this.fetchParams = { ...this.fetchParams, ...params };
    },
    // 新增
    addHandle() {
      this.deailType = 'add';
      this.formData = {};
      this.detailVisible = true;
    },
    // 数据操作
    async editHandle(row, type) {
      if (type != 'delete') {
        //编辑、明细
        this.deailType = type;
        this.detailVisible = true;
        this.formData = row;
      } else {
        //删除
        await this.$confirm('是否确定删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(async () => {
            const res = await deleteBigCustomer({ id: row.id });
            if (res.resultCode === 200) {
              this.$notify.success({ title: '提示信息', message: '删除成功' });
            }
            this.fetchParams = Object.assign({}, this.fetchParams);
          })
          .catch(() => {});
      }
    },
    detailcloseHandler() {
      this.detailVisible = false;
      if (this.deailType !== 'detail') {
        this.fetchParams = { ...this.fetchParams };
      }
    },
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '大客户名称',
          placeholder: '请输入',
          fieldName: 'customersName',
        },
        {
          type: 'INPUT',
          label: '大客户编号',
          placeholder: '请输入',
          fieldName: 'customersNo',
        },
        {
          type: 'RANGE_DATE',
          label: '提报时间',
          fieldName: 'reportDateBeginDate|reportDateEndDate',
          initialValue: '',
          format: 'yyyy-MM-dd',
          dateType: 'exactdaterange',
        },
        {
          type: 'SELECT',
          label: '状态',
          fieldName: 'status',
          placeholder: '请选择',
          itemList: this.createDictList('2146').filter((item) => item.value == '21461001' || item.value == '21461003'),
        },
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: (props) => {
            return <span>{props.row.index + 1}</span>;
          },
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 150,
          algin: 'center',
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" disabled={props.row.status === 21461004 || props.row.status === 21461002} onClick={() => this.editHandle(props.row, 'edit')}>
                  编辑
                </multiuse-button>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row, 'detail')}>
                  明细
                </multiuse-button>
                <multiuse-button size="mini" type="text" disabled={props.row.status != 21461001} onClick={() => this.editHandle(props.row, 'delete')}>
                  删除
                </multiuse-button>
              </div>
            );
          },
        },
        {
          title: '大客户来源',
          dataIndex: 'customersType',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: (p) => {
            return <span>{this.createDictText(p.row.customersType, '2108')}</span>;
          },
        },

        {
          title: '状态',
          dataIndex: 'status',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: (p) => {
            return <span>{this.createDictText(p.row.status, '2146')}</span>;
          },
        },
        {
          title: '大客户编号',
          dataIndex: 'customersNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          width: 200,
        },
        {
          title: '大客户名称',
          dataIndex: 'customersName',
          sorter: true,
          filter: true,
          filterType: 'input',
          width: 200,
        },
        {
          title: '运营单位联系人',
          dataIndex: 'contactsName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '运营单位联系电话',
          dataIndex: 'customerPhone',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '联系地址',
          dataIndex: 'addressName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '提报人',
          dataIndex: 'reportPersonName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '提报时间',
          dataIndex: 'reportDate',
          sorter: true,
          filter: true,
          filterType: 'input',
          dateFormat: 'yyyy-MM-dd',
        },
      ];
    },
  },
};
</script>